package com.dayuanit.controller;

import javax.servlet.http.HttpServletRequest;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.ResponseBody;

import com.dayuanit.dto.AjaxResultDTO;
import com.dayuanit.exception.MallException;
import com.dayuanit.service.CartService;
import com.dayuanit.vo.CartVO;

@Controller
@RequestMapping("/cart")
public class CartController {

	@Autowired
	private CartService cartService;
	
	@RequestMapping("/addProduct")
	@ResponseBody
	public AjaxResultDTO addProduct(CartVO cartVO, HttpServletRequest req) {
	
			try {
				if(null == cartVO.getProductCount() || 0 == cartVO.getProductCount()) {
					throw new MallException("商品数量不能为空");
				}
				if(null == cartVO.getProductId() || 0 == cartVO.getProductId()) {
					throw new MallException("商品不能为空1");
				}
				if(null == cartVO.getProductType() || "".equals(cartVO.getProductType())) {
					throw new MallException("商品不能为空2");
				}
				boolean flag = cartService.addCart(cartVO, req);
				if(true != flag) {
					throw new MallException("添加购物车中失败");
				}
			} catch (MallException e) {
				e.printStackTrace();
				return AjaxResultDTO.failed(e.getMessage());
			}		
		return AjaxResultDTO.success(null, null);
	}
	
	
}
